import { observable, action, autorun, toJS } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';
import {WeaForm, WeaTableNew} from 'comsMobx'
import {message, Button } from "antd";
import * as Apis from '../../apis/info';
import {Text} from '../../components/util/text';
const text = new Text();

const {TableStore} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
export class feedbackDialogStore {

    @observable feedbackForm = new WeaForm();      //信息反馈dialog
    @observable feedbackCondition = [];     //反馈信息弹框condition

    parent = null;
    constructor(parent) {
        this.parent = parent;
    }

    @observable status = {
        visible: false     //反馈信息dialog的visible
    }

    setState = (params = {}) => {
        Object.keys(params).forEach(o => {
            this.status[o] = params[o];
        })
        return this;
    }

    //初始化弹出框condition
    @action
    initFeedbackForm = (params = {}) => {
        this.feedbackCondition = [];
        this.feedbackForm = new WeaForm();
        let newParams = {...params };
        Apis.getInfoFeedbackCondition(newParams).then(data => {
            if (data.api_status == '1') {
                if (!data.isRight) {
                    message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                    return;
                }
                this.setState({ visible: true });
                this.feedbackCondition = data.infoFeedbackConditionGroup;
                this.feedbackForm && !this.feedbackForm.isFormInit && this.feedbackForm.initFormFields(this.feedbackCondition);
            } else {
                message.error(data.api_errormsg);
            }
        });
    }

    //提交反馈信息
    submitInfoFeedback = (params = {}) => {
        if (params.type == "0") {
            this.setState({ visible: false });
            return;
        }
        let formParams = this.feedbackForm.getFormParams();
        let newParams = {...formParams };
        this.feedbackForm.validateForm().then(f => {
            if (f.isValid) {
                Apis.submitInfoFeedback(newParams).then(data => {
                    if (data.api_status == '1') {
                        if (!data.isRight) {
                            message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                            return;
                        }
                        message.success(text.FEEDBACKINFO);
                        if(params.type == '1') {
                            //this.parent.initInfoForm({id:newParams.infoId,type:'show'});
                            window.close();   //关闭当前详情页
                            window.opener._table.reLoad();
                        }
                    } else {
                         message.error(data.api_errormsg);
                    }
                    this.setState({ visible: false });
                });
            } else {
                f.showErrors();
            }
        });
    }

}